<?php
class helper_paginator extends system_view_helper_abstract {
	protected $_paginator;
	protected $_script = 'paginator.phtml';
	protected $_paramPageKey = 'page';
	protected $_query = array();
	
	public function setParamPageKey($page) {
		$this->_paramPageKey = $page;
		return $this;
	}
	public function getParamPageKey() {
		return $this->_paramPageKey;
	}
	
	public function paginator(system_paginator $paginator) {
		$this->_paginator = $paginator;
		return $this;
	}
	
	public function setScript($script) {
		$this->_script = $script;
		return $this;
	}
	
	public function getControllerFront() {
		return system_controller_front::getInstance();
	}
	
	public function setQuery(array $query) {
		$this->_query = $query;
		return $this;
	}
	public function getQuery() {
		if(empty($this->_query)) {
			$this->setQuery($this->getControllerFront()->getRequest()->getQuery());
		}
		return $this->_query;
	}
	
	public function render($script = null, $route = null) {
		if($route === null) {
			$route = $this->getControllerFront()->getRouter()->getCurrentRoute();
		}
		if($script === null) {
			$script = $this->_script;
		}
		$data = array();
		$data['paginator'] = $this->_paginator;
		$data['route'] = $route;
		$data['params'] = $this->getControllerFront()->getRequest()->getParams();
		$data['query'] = $this->getQuery();
		$data['page'] = $this->getParamPageKey();
		return $this->getView()->partial($script, $data);
	}
	
	public function __toString() {
		return $this->render();
	}
}